package com.glink.gateway.common;

import cn.hutool.core.lang.Assert;
import com.alibaba.nacos.common.utils.ConvertUtils;
import com.glink.common.exception.GeneralSimpleException;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.server.reactive.ServerHttpRequest;

import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : qiushaoshan
 * @create 2024/6/26 9:50
 */
@Log4j2
public class SignatureValidator {

    public static SignatureWorker builder() {
        return new SignatureWorker();
    }

    public static class SignatureWorker {

        /**
         * 请求标识
         */
        private String nonce;
        /**
         * 请求时间
         */
        private Long timestamp;

        /**
         * 请求参数
         */
        private Map<String,Object> params;
        /**
         * 请求参数
         */
        private String resultBody;
        /**
         * 请求的签名数据
         */
        private String signData;

        /**
         * 私钥
         * 公钥--0431ee9e13950fe43cb0bd1a70cf1d515a45d3d7d61fa3d62ebf4f953583fc82eec8d2c2cf4b0f5673c374d15e4de5e69c7a19008c384d1ce2a26d25bba8c74fe2
         */
        private String privateKey = "a647648086c2d4a48b754e12ec034dc8f23b25ea4f0df9141dc43017787bc5ce";

        public SignatureWorker nonce(String nonce) {
            Assert.notNull(nonce, "请求标识不能为空");
            this.nonce = nonce;
            return this;
        }

        public SignatureWorker timestamp(Long timestamp) {
            this.timestamp = timestamp;
            return this;
        }

        public SignatureWorker params(Map<String, Object> parameterMap) {
            this.params = parameterMap;
            return this;
        }

        public SignatureWorker resultBody(String resultBody) {
            this.resultBody = resultBody;
            return this;
        }

        public SignatureWorker signData(String signData) {
            Assert.notNull(signData, "签名摘要不能为空");
            this.signData = signData;
            return this;
        }
 
        public SignatureWorker data(ServerHttpRequest request) {
            ReplayProperties.HeaderKey headerKey = ReplayProperties.props().getHeaderKey();
            return this.nonce(HttpUtils.getHeaderVal(request,headerKey.getNonce()))
                    .timestamp(ConvertUtils.toLong(HttpUtils.getHeaderVal(request,headerKey.getTimestamp())))
                    .signData(HttpUtils.getHeaderVal(request,headerKey.getSignData()));
        }

        public void execute() throws GeneralSimpleException {

//            String hash = Sm3EncryptionUtil.hash1(resultBody);
//
//            if (signData == null || !signData.equalsIgnoreCase(hash)) {
//                log.error("{}参数防篡改校验失败", "");
//                throw new GeneralSimpleException("200005");
//            }

            decryptParam(signData, "");
        }

        private String decryptParam(String resultBody, String typeMethodName) throws GeneralSimpleException {

            try {
                resultBody = resultBody.replace("\"", "");
                resultBody = Sm2EncryptionUtil.decrypt(privateKey, resultBody);
            }
            catch (Exception e) {
                log.error("{}参数解密失败", typeMethodName);
                throw new GeneralSimpleException("200002");
            }

            if (resultBody == null) {
                log.error("{}参数解密失败", typeMethodName);
                throw new GeneralSimpleException("200002");
            }
            return resultBody;
        }
    }
}
